package java6681.borrow;

import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public class BorrowWin6681 extends Application {
    private TextField tfStudent;
    private Label lblGet;

    private TextField tfBuy;

    @Override
    public void start(Stage primaryStage) {
        VBox root = new VBox(20);
        root.setStyle("-fx-background-color: palegoldenrod");

        HBox hName = new HBox(10);
        Label lblName = new Label("用户名：");
        TextField tfName = new TextField();
        tfName.setPromptText("输入用户名");
        hName.getChildren().addAll(lblName, tfName);

        HBox hPwd = new HBox(10);
        Label lalPwd = new Label("  密 码：");
        PasswordField pfPwd = new PasswordField();
        pfPwd.setPromptText("输入密码");
        hPwd.getChildren().addAll(lalPwd, pfPwd);

        tfName.setUserData("212006681");
        pfPwd.setUserData("zst6681");

        HBox hBut = new HBox(15);
        Button but_rua = new Button("登录");
        Button but_del = new Button("清除");
        hBut.setAlignment(Pos.CENTER);
        hBut.getChildren().addAll(but_del, but_rua);

        root.getChildren().addAll(hName, hPwd, hBut);
        root.setPadding(new Insets(20.0));
        root.setAlignment(Pos.TOP_CENTER);

        Scene scene = new Scene(root, 300, 160);
        primaryStage.setScene(scene);
        primaryStage.setTitle("登录");
        primaryStage.setResizable(false);
        primaryStage.show();

        but_del.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                tfName.setText("");
                pfPwd.setText("");
            }
        });

        but_rua.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
//                tfName.setUserData("用户名："+tfName.getText());
//                pfPwd.setUserData("密码："+pfPwd.getText());
//                System.out.println(tfName.getUserData()+"\n"+pfPwd.getUserData());

                String name = (String) tfName.getUserData();
                String password = (String) pfPwd.getUserData();

                if (name.equals(tfName.getText()) && password.equals(pfPwd.getText())) {
                    System.out.println("登录成功");
                    MyWindow myWindow = new MyWindow(name, password);
                    primaryStage.close();
                } else {
                    System.out.println("登录失败");

                    primaryStage.setTitle("错误");

                    FadeTransition fade = new FadeTransition();
                    fade.setDuration(Duration.seconds(0.5));
                    fade.setNode(root);
                    fade.setFromValue(0);
                    fade.setToValue(1);
                    fade.play();
                }
            }
        });
    }

    class MyWindow {
        private final Stage stage = new Stage();

        public MyWindow(String name, String password) {
            Text text = new Text("用户名：" + name + "   " + "密码:" + password);

            VBox root = new VBox(20);
            root.setStyle("-fx-background-color: bisque");

            HBox hPerson = new HBox(10);
            Label lblPerson = new Label("读者姓名：");
            tfStudent = new TextField();
            tfStudent.setPromptText("输入姓名");
            hPerson.getChildren().addAll(lblPerson, tfStudent);

            HBox hBuy = new HBox(10);
            Label lblBuy = new Label("图书名称：");
            tfBuy = new TextField();
            tfBuy.setPrefWidth(300);
            tfBuy.setPromptText("输入书名：");
            hBuy.getChildren().addAll(lblBuy, tfBuy);

            Button button = new Button("借书");
            lblGet = new Label();

            root.getChildren().addAll(text, hPerson, hBuy, button, lblGet);
            root.setPadding(new Insets(20.0));
            root.setAlignment(Pos.TOP_CENTER);

            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.setTitle("图书馆借书系统");
            stage.setHeight(300);
            stage.setWidth(500);
            stage.show();

            button.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {

                    if (tfStudent.getText().equals("") && tfBuy.getText().equals("")) {
                        lblGet.setText("");
                    } else {
                        lblGet.setText(tfStudent.getText() + "借到了：" + method(tfBuy.getText()));
                    }
                }
            });

        }

        public String method(String str) {
            String sb = "";
            Book6681 one = new Book6681("局外人");
            Book6681 two = new Book6681("鼠疫");
            Book6681 three = new Book6681("数据结构");
            Library6681 library = new Library6681(new Book6681[]{one, two, three}, "至诚学院");

            String[] split = str.split(",");
            Book6681[] book = new Book6681[split.length];

            for (int i = 0; i < book.length; i++) {
                if (i != book.length - 1) {
                    sb += library.borrow(split[i]).getName() + ",";
                } else {
                    sb += library.borrow(split[i]).getName();
                }
            }

            return sb;
        }
    }


    public static void main(String[] args) {
        launch(args);
    }
}
